package cn.kp.learning.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.kp.learning.model.CoreUserOrg;
import cn.kp.learning.model.CoreUserOrgExample;
import cn.kp.learning.model.CoreUserOrgExample.Criteria;
import cn.kp.learning.model.CoreUserOrgRole;
import cn.kp.learning.model.CoreUserOrgRoleExample;
import cn.kp.learning.model.People;
import cn.kp.learning.model.PeopleExample;
import cn.kp.learning.service.CoreUserOrgService;
import cn.kp.learning.service.CoreUserOrgRoleService;
import cn.kp.learning.service.PeopleService;
import cn.kp.learning.util.ResponseUtil;

@Controller
@RequestMapping("/people")
public class PeopleController {

	//private java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

	@Resource
	private PeopleService peopleService;

	@Resource
	private CoreUserOrgService cuoService;

	@Resource
	private CoreUserOrgRoleService cuorService;
	
	@RequestMapping("/peopleLogin")
	public ModelAndView peopleLogin(HttpServletResponse response,HttpServletRequest request,@CookieValue(value = "peopleCookie", defaultValue = "0") String peopleCookie ){

		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("userName", request.getParameter("userName"));
		map.put("passWord", request.getParameter("password"));
		
		System.out.println("log : " + request.getParameter("userName") + " = " + request.getParameter("password"));
		//user judge ...
		People pl = peopleService.selectPeopleLogin(map);
		if(null!= pl){
			System.out.println(pl.getUserName() +" is login ..." );
		}
		System.out.println("pl is null");
		//get user menus ...
		
		//get user roles ...
		
		//get user groups ...
		
		//set user session ... 
		return new ModelAndView("redirect:/pagecore/index.jsp");
	}
	@RequestMapping("/peopleExist")
	public void peopleExist(HttpServletResponse response,HttpServletRequest request, People pl) throws Exception{
		JSONObject json=new JSONObject();
		PeopleExample pExample = new PeopleExample();
		//pExample
		pExample.createCriteria().andUserNameLike(pl.getUserName());
		List<People> isExist = peopleService.selectPeopleList(pExample);
		System.out.println("search result : " + isExist.size());
		if(isExist.size()>0){
			json.put("isExist", true);
			json.put("success", false);
		}else{
			json.put("success", true);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/peopleAdd")
	public void peopleAdd(HttpServletResponse response,HttpServletRequest request, People pl) throws Exception{

		JSONObject json=new JSONObject();
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("userName", request.getParameter("userName"));
		map.put("passWord", request.getParameter("password"));
		
		System.out.println("adding person : " + pl.getAllName() +" - " + pl.getUserName());
		//user judge ...
		/*People pl = peopleService.getPeople(map);
		if(null!= pl){
			System.out.println(pl.getUserName() +" is login ..." );
		}*/
		// check before add
		PeopleExample pExample = new PeopleExample();
		//pExample
		pExample.createCriteria().andUserNameLike(pl.getUserName());
		List<People> isExist = peopleService.selectPeopleList(pExample);
		System.out.println("search result : " + isExist.size());
		if(isExist.size()>0){
			json.put("isExist", true);
			json.put("success", false);
		}else{
			int addResult = peopleService.addPeople(pl);
			System.out.println("adding person result :" + addResult + " ,search count : " + isExist.size());
			//get user menus ...
			
			//get user roles ...
			
			//get user groups ...
			
			//set user session ... 

			json.put("success", true);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println("get people size " + "1");
		//return lt;
	}
	@RequestMapping("/peopleModify")
	public void peopleModify(HttpServletResponse response,HttpServletRequest request, People pl) throws Exception{

		JSONObject json=new JSONObject();
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("userName", request.getParameter("userName"));
		map.put("passWord", request.getParameter("password"));
		
		System.out.println("adding person : " + pl.getAllName() +" - " + pl.getUserName());
		//user judge ...
		/*People pl = peopleService.getPeople(map);
		if(null!= pl){
			System.out.println(pl.getUserName() +" is login ..." );
		}*/
		// check before add
		//pExample
		Integer peopleUpdateResult = peopleService.updatePeople(pl);
		System.out.println("user update ....[Class:PeopleCOntroller] + [Method:peopleModify]");
		if(peopleUpdateResult>0){
			//int addResult = peopleService.updatePeople(pl);
			System.out.println("update result : " + peopleUpdateResult);
			//get user menus ...
			
			//get user roles ...
			
			//get user groups ...
			
			//set user session ... 
	
			json.put("success", true);
		}else{
			//modify user is not existed ...
			json.put("isExist", true);
			json.put("success", false);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println("get people size " + "1");
		//return lt;
	}
	@RequestMapping("/getPeopleList")
	//public List<People> getPeopleList(HttpServletResponse response){
	public void getPeopleList(HttpServletResponse response,HttpServletRequest request){
		System.out.println("get people list ... ");
		System.out.println("get para : " + request.getParameter("opportunityNO"));
		System.out.println("get para : " + request.getParameter("keyword"));
		System.out.println("get para : " + request.getParameter("search0"));
		System.out.println("get para : " + request.getParameter("search1"));
		System.out.println("get para : " + request.getParameter("search2"));
		System.out.println("get para : " + request.getParameter("search3"));
		System.out.println("get para : " + request.getParameter("search4"));
		System.out.println("get para : " + request.getParameter("search5"));
		System.out.println("get para : " + request.getParameter("limit"));
		System.out.println("get para : " + request.getParameter("start"));
		System.out.println("get para : " + request.getParameter("page"));
		System.out.println("get para : " + request.getParameter("pages"));

		List<People> lt = null;
		PeopleExample example = new PeopleExample();
		cn.kp.learning.model.PeopleExample.Criteria cc = example.createCriteria();
		
		String startRows = request.getParameter("start");
		String pagesLimit = request.getParameter("limit");
		if(null!=startRows && !startRows.equals("")){
			example.setStartRow(Integer.parseInt(startRows));
		}
		if(null!=pagesLimit && !pagesLimit.equals("")){
			example.setPageSize(Integer.parseInt(startRows) + Integer.parseInt(pagesLimit));
		}
		String search1 = request.getParameter("search1");
		String search2 = request.getParameter("search2");
		if(null!=search1 && !search1.equals("")){
			cc.andAllNameLike("%"+request.getParameter("search1") +"%");
		}
		if(null!=search2 && !search2.equals("")){
			cc.andUserNameLike("%"+request.getParameter("search2") + "%");
		}
		
		lt = peopleService.selectPeopleList(example);
		int countPeople = peopleService.countPeople(example);
		
		JSONObject json = new JSONObject();
		//
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("pages", lt.size()/10+1);
		json.put("total", countPeople);
		json.put("rows", jsonArray);
		
		System.out.println("people Json : " + json.toString());
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("get people size " + lt.size());
	}
	@RequestMapping("/getPeople")
	//public List<People> getPeopleList(HttpServletResponse response){
	public void getPeople(HttpServletResponse response,People pl){
		System.out.println("get people ... ");
		HashMap<String,Object> hm = new HashMap<String, Object>();
		if(null==pl){
			
		}else{
			hm.put("userName", pl.getUserName());
			System.out.println("" + pl.getUserName());
		}
		People p = peopleService.getPeople(hm);
		JSONObject json = new JSONObject();
		json.put("success", true);
		json.put("item", JSONObject.fromObject(p));
		
		System.out.println("people Json : " + json.toString());
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("get people Info " );
	}
	@RequestMapping("/addUserOrgRole")	
	public void addUserOrgRole(HttpServletResponse response,HttpServletRequest request,CoreUserOrg cuo, CoreUserOrgRole cuor){
		System.out.println("user organization and roles get ... " + cuor.getRolesId() + " - " + cuo.getOrgCompanyId()+ " - " + cuor.getOrgCompanyId());
		JSONObject json = new JSONObject();
		int addResult =0;
		if(null!=cuor){
			if(null==cuor.getRolesId() || cuor.getRolesId().equals("")){
				addResult = cuoService.addCuo(cuo);
			}else{
				System.out.println("user+org+role:["+ cuor.getRolesId()+"]");
				addResult = cuorService.addCuor(cuor);
			}
		}
		if(addResult==0){
			
		}else{
			json.put("success", true);
		}
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/deleteUserOrgRole")
	public void deleteUserOrgRole(HttpServletResponse response,HttpServletRequest request, CoreUserOrg cuo ,CoreUserOrgRole cuor){
		System.out.println("user organization and roles get ... " + cuor.getAllName());
	}
	@RequestMapping("/getUserOrgTable")
	public void getUserOrgTable(HttpServletResponse response,HttpServletRequest request, String userName){
		System.out.println("user organization and roles get ... " + userName);
		JSONObject json = new JSONObject();
		List<CoreUserOrg> lt = new ArrayList<CoreUserOrg>();
		if(null==userName || userName.equals("")){
			
		}else{
			CoreUserOrgExample example = new CoreUserOrgExample();
			Criteria criteria = example.createCriteria();
			criteria.andUserNameEqualTo(userName);
			lt = cuoService.getCuoList(example);
			
		}
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("pages", lt.size()/10+1);
		json.put("total", lt.size());
		json.put("rows", jsonArray);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/getUserOrgRoleTable")
	public void getUserOrgRoleTable(HttpServletResponse response,HttpServletRequest request, String userName){
		System.out.println("user organization and roles get ... " + userName);
		JSONObject json = new JSONObject();
		List<CoreUserOrgRole> lt = new ArrayList<CoreUserOrgRole>();
		if(null==userName || userName.equals("")){
			
		}else{
			CoreUserOrgRoleExample example = new CoreUserOrgRoleExample();
			cn.kp.learning.model.CoreUserOrgRoleExample.Criteria criteria = example.createCriteria();
			criteria.andUserNameEqualTo(userName);
			lt = cuorService.getCuorList(example);
			
		}
		JSONArray jsonArray = JSONArray.fromObject(lt);
		json.put("pages", lt.size()/10+1);
		json.put("total", lt.size());
		json.put("rows", jsonArray);
		
		try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
